package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.MessageTypeModel;
import com.yiwei.zxp.util.DateTime;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BussinessMsgAdapter extends ItemAdapter<MessageTypeModel.DatasBean, BussinessMsgAdapter.DefaultHolder> {


    private Context context;

    public BussinessMsgAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bussiness_msg, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        MessageTypeModel.DatasBean item = getItem(position);
        holder.txtTitle.setText(item.title);
        holder.txtContent.setText(item.content);
        holder.txtTime.setText(DateTime.toDateTime(Long.valueOf(item.created_at) * 1000));
        if (item.img != null && item.img.size() != 0) {
            Glide.with(context).load(item.img.get(0)).into(holder.imgIcon);
            holder.imgIcon.setVisibility(View.VISIBLE);
        } else {
            holder.imgIcon.setVisibility(View.GONE);
        }
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.order_no,item.child_type);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.layout_total)
        RelativeLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String order_no,String child_type);

    }
}
